package com.ruoyi.web.controller.workSafetyStandardization.standard;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.workSafetyStandardization.domain.StandardConfig;
import com.ruoyi.workSafetyStandardization.service.IItemConfigService;
import com.ruoyi.workSafetyStandardization.service.IMajorConfigService;
import com.ruoyi.workSafetyStandardization.service.IStandardConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 标准配置Controller
 *
 * @author hcc
 * @date 2023-12-14
 */
@RestController
@RequestMapping("/secure/securestandard")
public class StandardConfigController extends BaseController
{
    @Autowired
    private IStandardConfigService standardConfigService;
    @Autowired
    private IMajorConfigService majorConfigService;
    @Autowired
    private IItemConfigService itemConfigService;

    /**
     * 查询所有标准配置列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:list')")
    @GetMapping("/list")
    public TableDataInfo list(@Param("majorId") String majorId,@Param("itemId") String itemId,
                              @Param("pageSize") String pageSize,@Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        String[] Ids={majorId,itemId};
        List<StandardConfig> list =standardConfigService.selectStandardConfigByItemIdAndMajorId(Ids);
        return getDataTable(list);
    }
    /**
     * 获得专业Tree
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:tree')")
    @GetMapping( "/tree")
    public AjaxResult tree()
    {
        return success(majorConfigService.queryChildrenMajor(majorConfigService.queryAll()));
    }
//    /**
//     * 获得majorId专业下的所有项目名称
//     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:tree')")
//    @GetMapping( "/item/list")
//    public AjaxResult queryTree(@Param("majorId") String majorId,@Param("enable") Long enable)
//    {
////        StandardConfig standardConfig=new StandardConfig();
////        standardConfig.setMajorId(majorId);
////        standardConfig.setEnable(enable);
////        return success(standardConfigService.queryStandardByMajorIdAndEnable(standardConfig));
//        return success(itemConfigService.queryItemByMajor(majorId,enable));
//    }
//    /**
//     * 选择项目后增加新的项目内容
//     */
//    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:tree')")
//    @GetMapping( "/item/distribute")
//    public AjaxResult distribute(@Param("itemId") String itemId)
//    {
//        return success(standardConfigService.getOneItem(itemId));
//    }


    /**
     * 查找标准配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:query')")
    @GetMapping( "/query")
    public AjaxResult queryBy(@PathVariable String[] Ids)
    {
        return success(standardConfigService.selectStandardConfigByItemIdAndMajorId(Ids));
    }


    /**
     * 导出标准配置列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:export')")
    @Log(title = "标准配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StandardConfig standardConfig)
    {
        List<StandardConfig> list = standardConfigService.selectStandardConfigList(standardConfig);
        ExcelUtil<StandardConfig> util = new ExcelUtil<StandardConfig>(StandardConfig.class);
        util.exportExcel(response, list, "标准配置数据");
    }

    /**
     * 预览标准配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:preview')")
    @GetMapping("/preview/{standardId}")
    public AjaxResult previewInfo(@PathVariable("standardId") String standardId)
    {
        return success(standardConfigService.selectStandardConfigByStandardId(standardId));
    }


    /**
     * 新增标准配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:add')")
    @Log(title = "标准配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody StandardConfig standardConfig)
    {
        standardConfig.setStandardId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(standardConfigService.insertStandardConfig(standardConfig));
    }

    /**
     * 修改标准配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:edit')")
    @Log(title = "标准配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StandardConfig standardConfig)
    {
        return toAjax(standardConfigService.updateStandardConfig(standardConfig));
    }

    /**
     * 删除标准配置
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:standard:remove')")
    @Log(title = "标准配置", businessType = BusinessType.DELETE)
	@GetMapping ("/delete")
    public AjaxResult remove(@Param("standardId") String standardId)
    {
        String[] standardIds={standardId};
        return toAjax(standardConfigService.deleteStandardConfigByStandardIds(standardIds));
    }



}
